#include "drawable.h"
#include "frame.h"

Frame::Frame( const std::string& fn,
    Uint16 sprite_width, Uint16 sprite_height,
    Sint16 src_x, Sint16 src_y, bool setcolorkey )
  : spriteSurface(IOManager::getInstance()->loadAndSet(fn.c_str(), setcolorkey)), 
    screen( IOManager::getInstance()->getScreen() ),
    spriteWidth(sprite_width), spriteHeight(sprite_height),
    spriteSourceX(src_x), spriteSourceY(src_y) {

  if (!spriteSurface) { 
    throw std::string("Couldn't get Sprite surface: ")+fn; }

}

Frame::Frame( SDL_Surface* spr,
    Uint16 sprite_width, Uint16 sprite_height,
    Sint16 src_x, Sint16 src_y, bool setcolorkey )
  : spriteSurface(IOManager::getInstance()->loadAndSet(spr, setcolorkey)), 
    screen( IOManager::getInstance()->getScreen() ),
    spriteWidth(sprite_width), spriteHeight(sprite_height),
    spriteSourceX(src_x), spriteSourceY(src_y) {

  if (!spriteSurface) { 
    throw std::string("Couldn't get Sprite surface: "); }
}

void Frame::draw(Sint16 x, Sint16 y) const {
  SDL_Rect src = { spriteSourceX, spriteSourceY, 
                   spriteWidth, spriteHeight 
                 };    
  SDL_Rect dest = {x, y, spriteWidth, spriteHeight };
  SDL_BlitSurface(spriteSurface, &src, screen, &dest);
}

void Frame::draw(Sint16 sx, Sint16 sy, Sint16 dx, Sint16 dy) const {
  SDL_Rect src = { sx, sy, spriteWidth, spriteHeight };    
  SDL_Rect dest = {dx, dy, spriteWidth, spriteHeight };
  SDL_BlitSurface(spriteSurface, &src, screen, &dest);
}

